<template>
  <el-container class="groupon-view panel-block">
    <el-header class="sa-header">
      <el-tabs class="sa-tabs" v-model="filterParams.data.status" @tab-change="getData(1)">
        <el-tab-pane
          v-for="sl in statusList"
          :key="sl"
          :label="sl.name"
          :name="sl.type"
        ></el-tab-pane>
      </el-tabs>
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">拼团列表</span>
          <search-condition
            :conditionLabel="filterParams.conditionLabel"
            @deleteFilter="deleteFilter"
          ></search-condition>
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button class="sa-button-refresh" icon="Search" @click="openFilter"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="loading">
      <el-table height="100%" class="sa-table" :data="table.data" row-key="id" stripe>
        <template #empty>
          <sa-empty />
        </template>
        <el-table-column prop="id" label="ID" min-width="100" sortable> </el-table-column>
        <el-table-column label="拼团商品信息" min-width="240" align="center">
          <template #default="scope">
            <div class="sa-flex sa-row-center" v-if="scope.row.goods">
              <div class="sa-m-r-8">
                <sa-preview :url="scope.row.goods.image" size="40"></sa-preview>
              </div>
              <div class="sa-flex-col sa-col-top">
                <span
                  class="sa-table-line-1 goods-title cursor-pointer"
                  @click="onOpenGoodsDetail(scope.row.goods_id)"
                >
                  {{ scope.row.goods.title || '-' }}
                </span>
                <span class="sa-table-line-1 goods-subtitle"> 成团人数：{{ scope.row.num }} </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="开团时间" min-width="180" align="center">
          <template #default="scope">
            {{ scope.row.create_time || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="团长" min-width="124">
          <template #default="scope">
            <div class="sa-flex">
              <sa-user-profile :user="scope.row.user" :id="scope.row.user_id" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="已参团成员" min-width="180" align="center">
          <template #default="scope">
            <!-- <div class="sa-table-line-1"> -->
            <div class="sa-flex sa-row-center">
              <div v-for="i in scope.row.grouponLogs" :key="i" class="sa-m-r-4">
                <sa-preview :url="i.avatar" size="32" radius="16"></sa-preview>
              </div>
            </div>
            <!-- </div> -->
          </template>
        </el-table-column>
        <el-table-column label="剩余名额" min-width="90" align="center">
          <template #default="scope">
            {{ scope.row.num - scope.row.current_num }}
          </template>
        </el-table-column>
        <el-table-column label="组团有效时间" min-width="180" align="center">
          <template #default="scope">
            {{ scope.row.finish_time || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="拼团状态" min-width="88" align="center">
          <template #default="scope">
            <div
              :class="
                scope.row.status == 'invalid'
                  ? 'sa-color--danger'
                  : scope.row.status == 'ing'
                  ? 'sa-color--warning'
                  : 'sa-color--success'
              "
            >
              {{ scope.row.status_text || '-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="100">
          <template #default="scope">
            <el-button
              v-auth="'shop.admin.activity.groupon.detail'"
              class="is-link"
              type="primary"
              @click="detailRow(scope.row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from '../activity.service';
  import { useModal } from '@/sheep/hooks';
  import { useSearch } from '@/sheep/components/sa-table/sa-search/useSearch';
  import { composeFilter } from '@/sheep/utils';
  import { useRoute } from 'vue-router';
  import { cloneDeep } from 'lodash';
  import GoodsEdit from '@/app/shop/admin/goods/goods/edit.vue';
  import GrouponDetail from './detail.vue';
  const route = useRoute();
  // 列表
  const table = reactive({
    data: [],
    order: '',
    sort: '',
    selected: [],
  });
  const statusList = [
    {
      name: '全部',
      type: 'all',
    },
    {
      name: '进行中',
      type: 'ing',
    },
    {
      name: '已成团',
      type: 'finish',
    },
    {
      name: '虚拟成团',
      type: 'finish_fictitious',
    },
    {
      name: '已过期',
      type: 'invalid ',
    },
  ];
  const loading = ref(true);
  const filterParams = reactive({
    tools: {
      status: { label: '状态', value: 'all' },
      user: {
        type: 'tinputprepend',
        label: '团长信息',
        field: 'user',
        placeholder: '请输入查询内容',
        user: {
          field: 'user_id',
          value: '',
        },
        options: [
          {
            label: '团长ID',
            value: 'user_id',
          },
          {
            label: '团长昵称',
            value: 'user.nickname',
          },
          {
            label: '团长手机号',
            value: 'user.mobile',
          },
        ],
      },
      'goods.title': {
        type: 'tinput',
        label: '商品名称',
        field: 'goods.title',
        value: '',
      },
    },
    data: {
      user: { field: 'user_id', value: '' },
      status: 'all',
      'goods.title': '',
    },
    conditionLabel: {},
  });
  const { openFilter, deleteFilter } = useSearch({ filterParams, getData });
  // 获取
  async function getData() {
    loading.value = true;
    let tempSearch = cloneDeep(filterParams.data);
    if (route.query) {
      tempSearch['activity_id'] = route.query.activity_id;
    }
    let search = composeFilter(tempSearch, {
      'user.nickname': 'like',
      'goods.title': 'like',
    });
    const { error, data } = await api.groupon.list({
      order: table.order,
      sort: table.sort,
      ...search,
    });
    if (error === 0) {
      table.data = data.data;
    }
    loading.value = false;
  }

  function detailRow(row) {
    useModal(
      GrouponDetail,
      {
        title: '详情',
        id: row.id,
      },
      {
        confirm: () => {
          getData();
        },
        close: () => {
          getData();
        },
      },
    );
  }

  function onOpenGoodsDetail(id) {
    useModal(GoodsEdit, {
      title: '商品',
      type: 'edit',
      id: id,
    });
  }

  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  .groupon-view {
    .goods-title {
      color: var(--el-color-primary);
    }
    .goods-subtitle {
      font-size: 12px;
    }
  }
</style>
